<?php

class AdminUserController extends \BaseController 
{
	
	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{	
		$sort = Input::get('sort');
		$order = Input::get('order');
		
		if ($sort && $order) {
			$users = User::orderBy($sort, $order)->paginate(2);
		} else {
			$users = User::orderBy('id', 'desc')->paginate(2);
		}
		
		Debugbar::info('info');
		Debugbar::error('error');
		Debugbar::warning('warning');
		debug('info');
		
		if (Request::ajax()) {
			return View::make('admins.users.elements.table', compact('users', 'sort', 'order'));
		}
		
        return View::make('admins.users.index', compact('users', 'sort', 'order'));
	}


	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('admins.users.create');
	}


	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		$input = Input::all();
        $validation = Validator::make($input, User::$rules);

        if ($validation->passes()) {
            User::create($input);

            return Redirect::route('admin.user.index')
            	->with('message', 'Created successful.');
        }

        return Redirect::back()
            ->withInput()
            ->withErrors($validation);
	}


	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		echo 'show';
	}


	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$user = User::find($id);
		
        if (is_null($user)) {
            return Redirect::route('admin.user.index');
        }
        
        return View::make('admins.users.edit', compact('user'));
	}


	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$input = Input::all();
        $validation = Validator::make($input, User::$rules);
        
        if ($validation->passes()) {
            $user = User::find($id);
            $user->update($input);
            
            return Redirect::route('admin.user.index')
            	->with('message', 'Updated successful.');
        }
        
		return Redirect::route('admin.user.edit', $id)
            ->withInput()
            ->withErrors($validation);
	}


	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		User::find($id)->delete();
		
        return Redirect::route('admin.user.index')
        	->with('message', 'Deleted successful.');
	}
}
